package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Employee;
import com.sky.entity.OrderDetail;
import com.sky.mapper.AdminOrderMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminOrderService;
import com.sky.vo.DishVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    private AdminOrderMapper adminOrderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    /**
     * 订单搜索
     */
    public PageResult OrderSearch(OrdersPageQueryDTO dto) {
       /* //查询 total
        int i = adminOrderMapper.InquireCount(dto);
        //查询records
        Integer page = dto.getPage();
        Integer pageSize = dto.getPageSize();
        int index = (page - 1) * pageSize;
        List<OrderVO> orderVOS = adminOrderMapper.InquirePage(dto, index, pageSize);
        for (OrderVO orderVO : orderVOS) {
            orderVO.setOrderDetailList(orderDetailMapper.findById(orderVO.getId()));
        }*/
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<OrderVO> byAll = orderMapper.findByAll(dto);
        Page<OrderVO> page = (Page<OrderVO>) byAll;

        PageResult pageResult = PageResult.builder()
                .total(page.getTotal())
                .records(page.getResult())
                .build();

        return pageResult;
    }

    @Override
    /**
     * 各个状态的订单数量统计
     */
    public OrderStatisticsVO Statistics() {
        OrderStatisticsVO vo = new OrderStatisticsVO();
        vo.setToBeConfirmed(adminOrderMapper.CountStatus1());
        vo.setConfirmed(adminOrderMapper.CountStatus2());
        vo.setDeliveryInProgress(adminOrderMapper.CountStatus3());
        return vo;
    }
    @Override
    /**
     * 根据ID查询订单详情
     */
    public OrderVO finById(@PathVariable("id")String id) {
        OrderVO vo=adminOrderMapper.finById(id);
        List<OrderDetail> orderDetails = orderDetailMapper.findById(vo.getId());
        vo.setOrderDetailList(orderDetails);
        return vo;
    }

    @Override
    /**
     * 完成订单
     */
    public void complete(String id) {
        adminOrderMapper.complete(id);
    }

    @Override
    /**
     * 取消订单
     */
    public void cancel(OrdersCancelDTO dto) {

        adminOrderMapper.cancel(dto);
    }

    @Override
    /**
     * 接单
     */
    public void confirm(OrdersConfirmDTO dto) {
        adminOrderMapper.confirm(dto);
    }

    @Override
    /**
     * 拒单
     */
    public void rejection(OrdersRejectionDTO dto) {
        adminOrderMapper.rejection(dto);
    }

    @Override
    /**
     * 派送
     */
    public void delivery(String id) {
        adminOrderMapper.delivery(id);
    }
}
